import Mock from 'mockjs'

function param2Obj(url) {
    const search = url.split('?')[1]
    if (!search) {
        return {}
    }
    return JSON.parse('{"' + decodeURIComponent(search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') + '"}')
}

let List = []
const count = 60

for (let i = 0; i < count; i++) {
    List.push(Mock.mock({
        id: '@id',
        name: '@name',
        nickName: '@last',
        phone: /^1[34578]\d{9}$/,
        'age|11-99': 1,
        deptName: '技术部',
        isMale: '@boolean',
        email: '@email',
        createTime: '@datetime'
    }))
}


const tableAPI = {
     /**
     * 获取列表
     * 要带参数 name, page, limt; name可以不填, page,limit有默认值。
     * @param name, page, limit
     * @return {{code: number, count: number, data: *[]}}
     */
      getUserList: config => {
        const { name, page = 1, limit = 10 } = param2Obj(config.url)
        const mockList = List.filter(user => {
            if (name && user.name.indexOf(name) === -1) return false
            return true
        })
        const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))
        return {
            code: 20000,
            count: mockList.length,
            data: pageList
        }
    },
    /**
     * 增加用户
     * @param name, nickName, phone, deptName, isMale, email
     * @return {{code: number, data: {message: string}}}
     */
    createUser: config => {
        const { name, nickName, phone, deptName, isMale, email } = param2Obj(config.url)
        List.unshift({
            id: Mock.Random.guid(),
            name, 
            nickName, 
            phone, 
            deptName, 
            isMale: isMale === 'true', 
            email,
            createTime: new Date()
        })
        return {
            code: 20000,
            data: {
                message: '添加成功'
            }
        }
    },
    /**
     * 删除用户
     * @param id
     * @return {*}
     */
    deleteUser: config => {
        const { id } = param2Obj(config.url)
        if (!id) {
            return {
                code: -999,
                message: '参数不正确'
            }
        } else {
            List = List.filter(u => u.id !== id)
            return {
                code: 20000,
                data: {
                    message: '删除成功'
                }
            }
        }
    },
   
    /**
     * 修改用户
     * @param id, name, nickName, phone, deptName, isMale, email
     * @return {{code: number, data: {message: string}}}
     */
    updateUser: config => {
        const { id, name, nickName, phone, deptName, isMale, email } = param2Obj(config.url)
        List.forEach(u => {
            if (u.id === id) {
                u.name = name
                u.nickName = nickName
                u.phone = phone
                u.deptName = deptName
                u.isMale = isMale === 'true'
                u.email = email
            }
        })
        return {
            code: 20000,
            data: {
                message: '编辑成功'
            }
        }
    }
}

export default tableAPI
